package com.distribution.system.manager.channel.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.distribution.system.domain.bo.*;
import com.distribution.system.domain.dto.ChoosePhoneDTO;
import com.distribution.system.domain.dto.SendCodeDTO;
import com.distribution.system.domain.entity.*;
import com.distribution.system.domain.req.ReqResult;
import com.distribution.system.manager.channel.AbstractChannel;
import com.distribution.system.manager.channel.channelInfo.enums.HeBeiFlowChannelEnum;
import com.distribution.system.service.AddressChannelService;
import com.distribution.system.utils.http.HttpRequestUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: GodLu
 * @create: 2024-04-13 11:33
 * @description: 流量卡请求接口
 */
@Slf4j
public class FlowChannel extends AbstractChannel {
    public FlowChannel(CmccDaUpChannelInfoBO cmccDaUpChannelInfoBO) {
        super(cmccDaUpChannelInfoBO);
    }

    @Override
    public AddressInfoBo getChannelAddressInfo(AddressNameBo addressNameBo, AddressChannelService addressChannelService) {
        return null;
    }

    @Override
    public String configPlaceOrderRequestParams(CmccDaOrder cmccDaOrder, CmccDaProduct cmccDaProduct, AddressInfoBo addressInfoBo) {
        log.info("配置河北流量包下单接口参数...");
        // 生成订单号
        orderId = "LLB" + DateUtil.format(DateUtil.date(), "yyyyMMdd") + IdUtil.getSnowflake(1, 1).nextIdStr().substring(8,18);
        HashMap<String, Object> map = new HashMap<>(1 << 4);
        cmccDaOrder.setAddress(null);
        map.put("name", cmccDaOrder.getCustomerName());
        map.put("card", cmccDaOrder.getCustomerCertNo());
        map.put("productCode", cmccDaOrder.getProductParam());
        map.put("serverNumber", cmccDaOrder.getCustomerPhone());
        map.put("agencyCode", interfaceParamsMap.get(HeBeiFlowChannelEnum.AGENCY_CODE.getName()));
        map.put("password", interfaceParamsMap.get(HeBeiFlowChannelEnum.PASSWORD.getName()));
        map.put("smsRandomPass", cmccDaOrder.getCode());
        map.put("extentMsg", cmccDaOrder.getSubscribePath());
        return new JSONObject(map).toString();
    }

    @Override
    public String sendPlaceOrderRequest(String requestJsonStr) {
        return HttpRequestUtils.sendPost(interfaceUrlsMap.get(HeBeiFlowChannelEnum.PLACE_ORDER.getName()), requestJsonStr);
    }

    @Override
    public ReqResult parsePlaceOrderResponseStr(JSONObject responseJsonObject) {
        String resCode = responseJsonObject.getString("res_code");
        Integer code = responseJsonObject.getInteger("code");
        if (resCode == null && code == 500){
            return ReqResult.error(responseJsonObject.getString("msg"));
        }
        if ("0".equals(resCode)) {
            UnOrderResultBo unOrderResultBo = new UnOrderResultBo();
            unOrderResultBo.setOrderId(orderId);
            //String resultEndBody = responseJsonObject.getString("resultEndBody");
            unOrderResultBo.setChannelOrderNo(""); // 河北流量包暂无订单号
            return ReqResult.success(unOrderResultBo);
        } else {
            return ReqResult.error(responseJsonObject.getString("res_desc"));
        }
    }

    @Override
    public String configChoosePhoneRequestParams(ChoosePhoneDTO choosePhoneDTO, CmccDaProduct cmccDaProduct, AddressInfoBo addressInfoBo) {
        return null;
    }

    @Override
    public String sendChoosePhoneRequest(String requestJsonStr) {
        return null;
    }

    @Override
    public ReqResult parseChoosePhoneResponseStr(JSONObject responseJsonObject) {
        return null;
    }

    @Override
    public ReqResult sendCardSmsCode(CmccDaProduct cmccDaProduct, SendCodeDTO sendCodeDTO) {
        String param = "serverNumber=" +  sendCodeDTO.getCustomerPhone() + "&productCode=" + cmccDaProduct.getProductParam();
        String result = HttpRequestUtils.sendGet(interfaceUrlsMap.get(HeBeiFlowChannelEnum.SEND_SMS.getName()), param);
//        String result = "{\"res_code\":\"0\",\"res_desc\":\"Processing the request succeeded!\"}";
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONObject jsonObjectNew = new JSONObject();
        if ("0".equals(jsonObject.getString("res_code"))) {
            return ReqResult.success("发送成功");
        } else {
            return ReqResult.error(jsonObject.getString("res_desc"));
        }
    }

    @Override
    public RefreshOrderBO getRefreshOrderBO(CmccDaOrder cmccDaOrder) {
        return null;
    }

    @Override
    public Map<CmccDaOrder,RefreshOrderBO> getRefreshOrderBOMap(List<CmccDaOrder> orderList) {
        return null;
    }

    @Override
    public void refreshChannelCommission(CmccDaUpChannelInfo cmccDaUpChannelInfo) {
    }
}
